#ifndef __CONNECTION_HPP__
#define __CONNECTION_HPP__

/**
 *  This file is part of PennerBot
 *
 *  Copyright (C) 2010 microkernel
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <string>

#include <curl/curl.h>

#ifdef PBOT_CONSOLE

#include <console/statuslistener.hpp>
#define P_StatusListener PBot::Console::StatusListener

#elif defined(PBOT_GUI)

#include <gui/statuslistener.hpp>
#define P_StatusListener PBot::GUI::StatusListener

#endif

#include <core/config.hpp>

namespace PBot
{
namespace Core
{

class Connection
{
public:
    Connection(Config* cfg, P_StatusListener& listener);
    virtual ~Connection();

    bool Init();

    bool Get(const std::string& url, bool setreferer = true);
    bool GetFile(const std::string& url, const std::string& filename, bool setreferer = true);
    bool Post(const std::string& url, const std::string& urlencoded, bool setreferer = true);

    void SetReferer(const std::string& ref);
    std::string Escape(const std::string& data);

    const std::string& GetBuffer() const;

    size_t RealWriteBuffer(void *_buffer, size_t size, size_t nmemb);
    size_t RealWriteFile(void *_buffer, size_t size, size_t nmemb);

private:
    static size_t WriteBuffer(void *_buffer, size_t size, size_t nmemb, void *userp);
    static size_t WriteFile(void *_buffer, size_t size, size_t nmemb, void *userp);

    void GetLastError(const std::string& url);

    Config* cfg;
    P_StatusListener& listener;

    CURL* curl;
    CURLcode res;
    curl_slist* headers;

    std::string buffer;
    FILE* outfile;
};

}
}


#endif // __CONNECTION_HPP__
